package com.xjm.ips.exception;

import com.xjm.ips.entity.AjaxJson;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 处理数据关联异常
     * @return
     */
    @ExceptionHandler(SQLException.class)
    public AjaxJson SQLIntegrityConstraintViolationException(SQLException e){
        if(e instanceof SQLIntegrityConstraintViolationException){
            System.out.println(e.toString());
            return AjaxJson.error("操作数据不满足数据库约束条件，操作失败!");
        }else {
            System.out.println(e.toString());
            return AjaxJson.error("数据库异常，操作失败！");
        }
    }
}
